package zhr.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhr.store.common.BaseContext;
import zhr.store.common.Result;
import zhr.store.domain.ShoppingCart;
import zhr.store.service.ShoppingCartService;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     *根据条件查询购物车
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //构建查询条件
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        //查询
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return Result.success(list);
    }
    /**
     * 添加购物车
     *
     * @return
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //查询购物车是否有当前菜品或套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询当前用户购物车
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        if (dishId != null) {
            //当前添加菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //当前添加套餐
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (one != null) {
            //数量增加
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //添加
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            one = shoppingCart;
            shoppingCartService.save(one);
        }
        return Result.success("添加成功");
    }
    /**
     * 根据id清除购物车
     */
    @PostMapping("/sub")
    public Result<String> sub(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断删除套餐还是菜品
        if (shoppingCart.getDishId() != null) {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
                    .eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
            //判断数量是否为1
            Integer number = one.getNumber();
            if (number > 1) {
                //数量减1
                one.setNumber(one.getNumber() - 1);
                shoppingCartService.updateById(one);
            } else {
                shoppingCartService.removeById(one);
            }
        } else if (shoppingCart.getSetmealId() != null) {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId)
                    .eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
            //判断数量是否为1
            Integer number = one.getNumber();
            if (number > 1) {
                //数量减1
                one.setNumber(one.getNumber() - 1);
                shoppingCartService.updateById(one);
            } else {
                shoppingCartService.removeById(one);
            }
        }
        return Result.success("删除成功");
    }
    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public Result<String> clean() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //构建查询条件
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        //清空购物车
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return Result.success("删除成功");
    }
}
